﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ECCIT;
using ECCIT.Data;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Utility;

namespace OgilvyOne.UKNow.DAL
{
    public class MemberReminderDAL : UKNowBaseDAL, IDALMemberReminder
    {
        private const string TBL_NAME = "UKNOW_MEMBER_REMINDER_MAPPING";
        private const string MEMBER_ID = "MEMBER_ID";
        private const string COL_EVENTS_ID = "COL_EVENTS_ID";
        private const string COL_LANG_ID = "COL_LANG_ID";
        public override string TableName { get { return TBL_NAME; } }
        public override Type ModelType { get { return typeof(MemberReminderMappingModel); } }
        [PrimaryKey]
        public IField LanguageID { get { return FieldFactory.Create(ModelType, "COL_LANG_ID", ColumnFactory.CreateColumn(COL_LANG_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField Memberid { get { return FieldFactory.Create(ModelType, "MEMBER_ID", ColumnFactory.CreateColumn(MEMBER_ID, DataType.Int)); } }
        [PrimaryKey]
        public IField Eventid { get { return FieldFactory.Create(ModelType, "COL_EVENTS_ID", ColumnFactory.CreateColumn(COL_EVENTS_ID, DataType.Int)); } }
        public override IField[] InsertFields { get { return new IField[] { Memberid, Eventid,LanguageID }; } }
        public override IField[] SelectFields { get { return null; } }
        public override IField[] UpdateFields { get { return null; } }

         public MemberReminderDAL() { }

         public MemberReminderDAL(LanguageType languageType) { this.Language = LanguageFactory.Create(languageType); }

         /// <summary>
         /// 清除提醒
         /// </summary>
         /// <param name="mappingModel"></param>
         /// <returns></returns>
         public ResultInfo Delete(MemberReminderMappingModel mappingModel)
         {

             var re = DeleteModelByPrimaryKey(this.GetType(),
                                              new KeyValuePair<IField, object>[]
                                                  {
                                                      new KeyValuePair<IField, object>(this.LanguageID,
                                                                                       mappingModel.COL_LANG_ID),
                                                      new KeyValuePair<IField, object>(this.Eventid,
                                                                                       mappingModel.COL_EVENTS_ID),
                                                      new KeyValuePair<IField, object>(this.Memberid,
                                                                                       mappingModel.MEMBER_ID)
                                                  });
             return re;
         }

        public ResultInfo Save(MemberReminderMappingModel modelInfo)
         {
             var info = new ResultInfo();
             if (CheckExist(modelInfo))
             {
                 info.Message = Constants.IsExist;
                 info.Result = false;
                 return info;

             }
             if (Insert(modelInfo, null) > 0)
             {
                 info.Message = Constants.Success;
                 info.Result = true;
                 return info;
             }
             return null;
         }

         /// <summary>
         /// 插入提醒
         /// </summary>
         /// <param name="modelInfo"></param>
         /// <returns></returns>
         public ResultInfo Insert(MemberReminderMappingModel modelInfo)
         {
             var info = new ResultInfo();
             if (Insert(modelInfo, null) > 0)
             {
                 info.Message = Constants.Success;
                 info.Result = true;
                 return info;
             }
             return null;
         }

        /// <summary>
        /// 清除所有提醒
        /// </summary>
        /// <param name="mappingModel"></param>
        public void ClearAll(MemberReminderMappingModel mappingModel)
         {
             IDataParameter[] parameters = {
					new SqlParameter("@lanagerid", SqlDbType.Int,4),
					new SqlParameter("@memberid", SqlDbType.Int,4)
					};
            parameters[0].Value = mappingModel.COL_LANG_ID;
            parameters[1].Value = mappingModel.MEMBER_ID;
            DBHelper.ExecuteNonQuery(
                "delete from  UKNOW_MEMBER_REMINDER_MAPPING  where COL_LANG_ID=@lanagerid and MEMBER_ID=@memberid",
                parameters);

         }




        private bool CheckExist(MemberReminderMappingModel modelInfo)
         {
             string whereClause = string.Format("where {0}={1} and {2}={3} and {4}={5}", MEMBER_ID, modelInfo.MEMBER_ID, COL_EVENTS_ID, modelInfo.COL_EVENTS_ID, COL_LANG_ID, modelInfo.COL_LANG_ID);

             using (IDataReader reader = DBHelper.ExecuteReader("select count(1) from UKNOW_MEMBER_REMINDER_MAPPING " + whereClause))
             {
                 while (reader.Read())
                 {
                     return Convert.ToInt32(reader[0]) > 0;
                 }
             }
             return false;
             //return GetModelByPrimaryKey(this.GetType(),
             //                             new KeyValuePair<IField, object>[]
             //                                 {
             //                                     new KeyValuePair<IField, object>(this.Memberid, modelInfo.MEMBER_ID),
             //                                     new KeyValuePair<IField, object>(this.Eventid, modelInfo.COL_EVENTS_ID)
             //                                     ,
             //                                     new KeyValuePair<IField, object>(this.LanguageID,
             //                                                                      modelInfo.COL_LANG_ID)
             //                                 }) != null;

         }
    }
}
